    //
    //  UIImageView+TZExtension.m
    //  TZOCExtension
    //
    //  Created by Tim.Z on 2017/1/3.
    //  Copyright © 2017年 Tim.Z. All rights reserved.
    //

#import "UIImageView+TZExtension.h"



@implementation UIImageView (TZExtension)

- (void)tz_setupImgWithKImageType:(KTZImgType)type{
    
    NSInteger cornerRadius = 2;
    
    switch (type) {
        case KTZImgType_circle:
        {
        cornerRadius = self.frame.size.width/2;
        }
            break;
            
        default:
            break;
    }
    
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}

+ (UIImage *)tz_drawImaginaryLineByImageView:(UIImageView *)imageView withKTZDrawImaginaryLineType:(KTZDrawImaginaryLineType)type{
    UIGraphicsBeginImageContext(imageView.frame.size); //开始画线 划线的frame
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
        //设置线条终点形状
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 0.1);
        // 5是每个虚线的长度 1是高度
    CGFloat lengths[2] = { 3, 5 };
    CGContextRef line = UIGraphicsGetCurrentContext();
        // 设置颜色
    CGContextSetStrokeColorWithColor(line, UIColorFromRGB(0x333333).CGColor);
    
    CGContextSetLineDash(line, 0, lengths, 1);//画虚线
    
    switch (type) {
        case KTZDrawImaginaryLineType_verticlal:
        {
        CGContextMoveToPoint(line, imageView.frame.size.width/2, 8); //开始画线
        CGContextAddLineToPoint(line, imageView.frame.size.width/2, imageView.frame.size.height-8);
        }
            break;
        case KTZDrawImaginaryLineType_horizontal:
        {
        CGContextMoveToPoint(line, 5, imageView.frame.size.height/2); //开始画线
        CGContextAddLineToPoint(line, imageView.frame.size.width-5, imageView.frame.size.height/2);
        }
            break;
            
        default:
            break;
    }
    
    CGContextStrokePath(line);
        // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}

- (void)tz_becomeImaginaryLineWithKTZDrawImaginaryLineType:(KTZDrawImaginaryLineType)type{
    self.image = [UIImageView tz_drawImaginaryLineByImageView:self withKTZDrawImaginaryLineType:type];
}

@end
